﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForfiterAmp.WinClient.WinAmpService;
using System.ServiceModel;

namespace ForfiterAmp.WinClient
{
    public partial class MainForm : Form
    {
        private const string SERVER_STATUS_DISCONNECTED = "Rozłączony...";

        private const string SERVER_STATUS_CONNECTED = "Połączony: ";

        private const string SERVER_ACK = "trolololo";

        private PlayerStateListener _playerStateListener;

        /// <summary>
        /// Gets or sets the win amp service client.
        /// </summary>
        /// <value>The win amp service client.</value>
        public static WinAmpServiceSoapClient WinAmpServiceClient { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        public MainForm()
        {
            _playerStateListener = new PlayerStateListener();
            InitializeComponent();
        }

        private void UpdatePlayerState()
        {
            if (InvokeRequired)
                BeginInvoke(new Action(UpdatePlayerState));
            else
            {
                txtTitle.Text = _playerStateListener.CurrentPlayerState.SongState.CurrentSong.Title;
            }
        }

        /// <summary>
        /// Handles the Load event of the MainForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            Connect();
            _playerStateListener.PlayerStateChanged += PlayerStateListener_PlayerStateChanged;
            _playerStateListener.ServiceErrorOccured += new UnhandledExceptionEventHandler(_playerStateListener_ServiceErrorOccured);
        }

        void _playerStateListener_ServiceErrorOccured(object sender, UnhandledExceptionEventArgs e)
        {
            HandleServerError();
        }

        private void PlayerStateListener_PlayerStateChanged(object sender, EventArgs e)
        {
            UpdatePlayerState();
        }

        /// <summary>
        /// Handles the Click event of the btnPrev control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnPrev_Click(object sender, EventArgs e)
        {
            try
            {
                WinAmpServiceClient.PreviousTrack();
            }
            catch
            {
                HandleServerError();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnPlay control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnPlay_Click(object sender, EventArgs e)
        {
            try
            {
                WinAmpServiceClient.Play();
            }
            catch
            {
                HandleServerError();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnPause control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnPause_Click(object sender, EventArgs e)
        {
            try
            {
                WinAmpServiceClient.Pause();
            }
            catch
            {
                HandleServerError();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnStop control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            try
            {
                WinAmpServiceClient.Stop();
            }
            catch
            {
                HandleServerError();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnNext control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                WinAmpServiceClient.NextTrack();
            }
            catch
            {
                HandleServerError();
            }
        }

        /// <summary>
        /// Handles the Click event of the buttonVolumeMinus control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonVolumeMinus_Click(object sender, EventArgs e)
        {
            try
            {
                WinAmpServiceClient.VolumeDown();
            }
            catch
            {
                HandleServerError();
            }
        }

        /// <summary>
        /// Handles the Click event of the buttonVolumePlus control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonVolumePlus_Click(object sender, EventArgs e)
        {
            try
            {
                WinAmpServiceClient.VolumeUp();
            }
            catch
            {
                HandleServerError();
            }
        }

        private void DisablePlayerButtons()
        {
            btnNext.Enabled = false;
            btnPause.Enabled = false;
            btnPlay.Enabled = false;
            btnPrev.Enabled = false;
            btnStop.Enabled = false;
            btnVolumeMinus.Enabled = false;
            btnVolumePlus.Enabled = false;
        }

        private void EnablePlayerButtons()
        {
            btnNext.Enabled = true;
            btnPause.Enabled = true;
            btnPlay.Enabled = true;
            btnPrev.Enabled = true;
            btnStop.Enabled = true;
            btnVolumeMinus.Enabled = true;
            btnVolumePlus.Enabled = true;
        }

        private void HandleServerError()
        {
            if (InvokeRequired)
                BeginInvoke(new Action(HandleServerError));
            else
            {
                _playerStateListener.Stop();
                DisablePlayerButtons();
                lblServerStatus.Text = SERVER_STATUS_DISCONNECTED;
            }
            
        }

        private void TestConnection()
        {
            try
            {
                if (WinAmpServiceClient.HelloWorld() == SERVER_ACK)
                {
                    lblServerStatus.Text = SERVER_STATUS_CONNECTED + WinAmpServiceClient.Endpoint.Address.Uri.Authority;
                    EnablePlayerButtons();
                    _playerStateListener.Start();
                }
                else
                {
                    HandleServerError();
                    MessageBox.Show("Nie można nawiązać połączenia z serwerem.");
                }
            }
            catch
            {
                HandleServerError();
                MessageBox.Show("Nie można nawiązać połączenia z serwerem.");
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            Connect();
        }

        private void Connect()
        {
            WinAmpServiceClient = new WinAmpServiceSoapClient();
            TestConnection();
        }
    }
}
